package com.whut.service.impl;

import com.whut.mapper.NoticeinfoMapper;
import com.whut.mapper.UserinfoMapper;
import com.whut.model.Noticeinfo;
import com.whut.model.Userinfo;
import com.whut.service.NoticeService;
import com.whut.websocket.WebSocketServer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("noticeService")
public class NoticeServiceImp implements NoticeService {
    @Resource
    NoticeinfoMapper noticeinfoMapper;
    @Resource
    UserinfoMapper userinfoMapper;

    /*wyh*/
    @Override
    public Object getAllNotice() {
        List<Noticeinfo> noticeinfoList =noticeinfoMapper.getAllNotice();
        return noticeinfoList;
    }

    /*wyh*/
    @Override
    public Object getAdminById(int id) {
        List<Noticeinfo> noticeinfoList = noticeinfoMapper.getNoticeById(id);
        return noticeinfoList;
    }

    /*wyh*/
    @Override
    public Object getNoticeByTime(String startTime, String endTime, int id) {
        List<Noticeinfo> noticeinfoList = noticeinfoMapper.getNoticeByTime(startTime, endTime, id);
        return noticeinfoList;
    }

    /*wyh*/
    @Override
    public Object addNotice(int id, String userName, String notice, String time) {
        Noticeinfo noticeinfo = new Noticeinfo();
        noticeinfo.setId(id);
        noticeinfo.setNotice(notice);
        noticeinfo.setTime(time);
        noticeinfo.setUserName(userName);
        try{
            noticeinfoMapper.insertSelective(noticeinfo);
        }catch (Exception e){
            return false;
        }
        List<Userinfo> list = userinfoMapper.selectAllStaff();
        for (Userinfo user:list) {
            WebSocketServer ws = WebSocketServer.getInstance(user.getId());
            if(ws!=null){
                ws.sendMessage("您有一个新的通知");
            }
        }
        List<Noticeinfo> noticeinfoList =noticeinfoMapper.getAllNotice();
        return noticeinfoList;
    }
}
